﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using WpfHero;

namespace WpfHeroResponse
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public WHEngine Engine { get; set; }

        private Storyboard showLoading;
        private Storyboard startPlaying;

        public Window1()
        {
            InitializeComponent();
            Engine = scroller.DataContext as WHEngine;
            showLoading = (Storyboard)Resources["ShowLoading"];
            startPlaying = (Storyboard)Resources["StartPlaying"];
        }

        private void lbSongBrowser_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SongBrowserDirSelected();
        }

        private void lbSongBrowser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                SongBrowserDirSelected();
        }

        private void SongBrowserDirSelected()
        {
            SongBrowser sb = (SongBrowser)((ObjectDataProvider)Resources["SongBrowserDS"]).ObjectInstance;
            var selectedDir = (MyDirectory)lbSongBrowser.SelectedItem;
            sb.CurrentDirectory = selectedDir;

            if (selectedDir.HasSong)
                StartGame(selectedDir.FullPath);
        }

        private bool storyboardsInitialized = false;
        private void StartGame(string path)
        {
            Engine.Stop();

            if (!storyboardsInitialized)
            {
                showLoading.Completed += delegate
                {
                    Engine.Initialize(path, 1);
                    //TODO: startplaying does not play
                    App.DoEvents();
                    startPlaying.Begin(this);
                };
                startPlaying.Completed += delegate
                {
                    scroller.Start();
                    Engine.Start();
                };
                storyboardsInitialized = true;
            }
            showLoading.Begin(this);

            //if (!storyboardsInitialized)
            //{
            //    showLoading.Completed += delegate
            //    {
            //        BackgroundWorker bw = new BackgroundWorker();
            //        bw.DoWork += (sender, e) =>
            //                         {
            //                             Engine.Initialize(path, 1);
            //                         };
            //        bw.RunWorkerCompleted += (sender, e) =>
            //                                     {
            //                                         App.DoEvents();
            //                                         startPlaying.Begin(this);
            //                                     };
            //        bw.RunWorkerAsync();
            //    };
            //    startPlaying.Completed += delegate
            //    {
            //        scroller.Start();
            //        Engine.Start();
            //    };
            //    storyboardsInitialized = true;
            //}
            //showLoading.Begin(this);
        }
    }
}
